﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using QuanLyDoiXe.lop_doi_tuong;
using QuanLyDoiXe.Lop_du_lieu;
using System.IO;
using BarcodeLib;

namespace QuanLyDoiXe
{
    public partial class frmbaocaosudungnhienlieu : Form
    {
        SqlConnection conn = new SqlConnection("Data Source=ACER;Initial Catalog=quanlydoixevinamilk;Integrated Security=True");
        public frmbaocaosudungnhienlieu()
        {
            InitializeComponent();
        }
        #region 1. thoát & nhập lại
        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnnhaplai_Click(object sender, EventArgs e)
        {
            txtdinhmuc.Text = "";
            txtquangduong.Text = "";
            txtxangtieuthu.Text = "";
            txtxe.Text = "";
            cbbthangbc.ResetText();
        }
        #endregion
        #region 2. hiển thị bao cao
        ListViewItem item;
        private void HienThiBCSDNL()
        {
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("Select * from bcsudungnhienlieu,xecongty,dinhmuc where bcsudungnhienlieu.Mxe = xecongty.Mxe and bcsudungnhienlieu.MDM=dinhmuc.MDM ", conn);
                SqlDataReader sqlDA = cmd.ExecuteReader();
                int i = 0;
                lvbcsudungnhienlieu.View = View.Details;
                lvbcsudungnhienlieu.FullRowSelect = true;
                while (sqlDA.Read())
                {
                    i++;
                    item = new ListViewItem(i.ToString());
                    item.SubItems.Add(sqlDA["Mxe"].ToString());
                    item.SubItems.Add(sqlDA["Dinhmuc"].ToString());
                    item.SubItems.Add(sqlDA["Quangduong"].ToString());
                    item.SubItems.Add(sqlDA["Xangtieuthu"].ToString());
                    item.SubItems.Add(sqlDA["Thangbc"].ToString());
                    lvbcsudungnhienlieu.Items.Add(item);
                }
                cmd.Dispose();
                sqlDA.Close();
                conn.Close();
            }
            catch
            {
            }
        }
        private void lvbcsudungnhienlieu_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            ListViewItem item = e.Item;
            txtxe.Text = item.SubItems[1].Text;
            txtdinhmuc.Text = item.SubItems[2].Text;
            txtquangduong.Text = item.SubItems[3].Text;
            txtxangtieuthu.Text = item.SubItems[4].Text;
            cbbthangbc.Text = item.SubItems[5].Text;
        }
        string Mxe, Quangduong;
        int Dinhmuc, Xangtieuthu, Thangbc;
        private void lvbcsudungnhienlieu_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < lvbcsudungnhienlieu.Items.Count; i++)
            {
                if (lvbcsudungnhienlieu.Items[i].Selected)
                {
                    Mxe = item.SubItems[1].Text;
                    Dinhmuc =Convert.ToInt32(item.SubItems[2].Text);
                    Quangduong = item.SubItems[3].Text;
                    Xangtieuthu = Convert.ToInt32(item.SubItems[4].Text);
                    Thangbc = Convert.ToInt32(item.SubItems[5].Text);
                }
            }
        }

        private void frmbaocaosudungnhienlieu_Load(object sender, EventArgs e)
        {
            HienThiBCSDNL();
        }
        #endregion
        #region 3. cập nhập
        private void btncapnhap_Click(object sender, EventArgs e)
        {
            string strMxe = "";
            Lop_lay_du_lieu dl = new Lop_lay_du_lieu();
            SqlDataReader dr = dl.lay_reader("select Mxe from xecongty where Mxe=N\'" + txtxe.Text + "\'");
            while (dr.Read())
            {
                strMxe = dr.GetString(0);
            }
            dr.Close(); dr.Dispose();
            string strdinhmuc = "";
            Lop_lay_du_lieu dl1 = new Lop_lay_du_lieu();
            SqlDataReader dr1 = dl1.lay_reader("select MDM from dinhmuc where Dinhmuc='" + txtdinhmuc.Text + "'");
            while (dr1.Read())
            {
                strdinhmuc = dr1.GetString(0);
            }
            dr1.Close(); dr1.Dispose();

            lop_ket_noi.huyketnoi();
            if (txtxe.Text != ""  && txtquangduong.Text != "" && txtdinhmuc.Text !=  "" && txtxangtieuthu.Text != "" && cbbthangbc.Text != "" )
            {
                lop_bcsudungnhienlieu nhienlieu = new lop_bcsudungnhienlieu(strMxe,txtquangduong.Text,strdinhmuc,Convert.ToInt32(txtxangtieuthu.Text),Convert.ToInt32(cbbthangbc.Text));
                if (nhienlieu.capnhat())
                    MessageBox.Show("Quá trình cập nhật báo cáo thành công!");
                else
                    MessageBox.Show("Quá trình cập nhật báo cáo thất bại!");
            }
            else
                MessageBox.Show("Bạn hãy nhập đầy đủ thông tin báo cáo! ");
            lvbcsudungnhienlieu.Items.Clear();
            HienThiBCSDNL();
        }
        #endregion
        #region 4. in bao cao
        public static string Xechay = "";
        public static string QD = "";
        public static string DM = "";
        public static string Xtieuthu = "";
        public static string TBC = "";
        private void btnxuat_Click(object sender, EventArgs e)
        {
            if (txtxe.Text != "" && txtquangduong.Text != "" && txtdinhmuc.Text != "" && txtxangtieuthu.Text != "" && cbbthangbc.Text != "" )
            {
                Xechay = txtxe.Text;
                QD = txtquangduong.Text;
                DM = Convert.ToString(txtdinhmuc.Text);
                Xtieuthu = Convert.ToString(txtxangtieuthu.Text);
                TBC = Convert.ToString(cbbthangbc.Text);
                frminbaocaosudungnhienlieu frminbaocao = new frminbaocaosudungnhienlieu();
                frminbaocao.Show();
            }
            else
            { }
        }
        #endregion
    }
}
